package com.lebang.medicalInquiry.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * redis 的第一个数据源，再有数据源，以此类推
 *
 * @author : tony
 * @Date: 2019-02-22 09:55
 * @Description: 默认redis
 */
@Configuration
@EnableCaching
public class DefaultRedisConfig extends BaseRedisConfig {
    @Value("${spring.redis.database}")
    private int dbIndex;

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private int port;

    @Value("${spring.redis.password}")
    private String password;


    /**
     * 配置redis连接工厂
     *
     * @return
     */
    @Primary
    @Bean
    public RedisConnectionFactory defaultRedisConnectionFactory() {
        return super.createJedisConnectionFactory(dbIndex, host, port, password);
    }

    /**
     * 配置redisTemplate 注入方式使用@Resource(name="") 方式注入
     * name 是调用引入的名称，需保证其唯一性
     *
     * @return
     */
    @Primary
    @Bean(name = "defaultRedisTemplate")
    public RedisTemplate defaultRedisTemplate() {
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(defaultRedisConnectionFactory());
        setSerializer(template);
        template.afterPropertiesSet();
        return template;


    }

}
